package br.com.tenax.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.tenax.entity.Funcionario;


public class FuncionarioDAOimp implements FuncionarioDAO {

	@Override
	public void incluir(Funcionario funcionario) {
		Connection conexao = null;

		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao
					.prepareStatement("INSERT INTO tbfuncionario(id, nome, cpf, comissao, datainclusao, status, setor) "
							+ "VALUES (?, ?, ?, ?, ?, ?, ?)");

			pstm.setInt(1, funcionario.getId());
			pstm.setString(2, funcionario.getNome());
			pstm.setString(3, funcionario.getCpf());
			pstm.setDouble(4, funcionario.getComissao());
			pstm.setDate(5, new java.sql.Date(funcionario.getDataInclusao().getTime()));
			pstm.setString(6, funcionario.getStatus());
			pstm.setString(7, funcionario.getSetor());
			pstm.executeUpdate();
			pstm.close();
			conexao.close();
		} catch (Exception e) {
			System.out.println(e);
		}

	}

	@Override
	public void atualizar(Funcionario funcionario) {
		Connection conexao = null;

		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao
					.prepareStatement("UPDATE tbfuncionario SET nome=?, cpf=?, comissao=?, datainclusao=?, status=?, setor=? "
							+ "WHERE id =?");

			pstm.setString(1, funcionario.getNome());
			pstm.setString(2, funcionario.getCpf());
			pstm.setDouble(3, funcionario.getComissao());
			pstm.setDate(5, (Date) funcionario.getDataInclusao());
			pstm.setString(6, funcionario.getStatus());
			pstm.setString(7, funcionario.getSetor());
			pstm.setInt(8, funcionario.getId());
			pstm.executeUpdate();
			pstm.close();
			conexao.close();
		} catch (Exception e) {
			System.out.println(e);
		}

	}

	@Override
	public void deletar(Funcionario funcionario) {
		Connection conexao = null;

		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao
					.prepareStatement("DELETE FROM tbfuncionario WHERE id=?");
			pstm.setInt(1, funcionario.getId());
			pstm.executeUpdate();
			pstm.close();
			conexao.close();
		} catch (Exception e) {
			System.out.println(e);
		}

	}

	@Override
	public Funcionario consultar(Funcionario funcionario) {
		Funcionario f = new Funcionario();
		Connection conexao = null;

		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao
					.prepareStatement("SELECT id, nome, cpf, comissao, datainclusao, status, setor FROM tbfuncionario where id= ?");
			pstm.setInt(1, funcionario.getId());

			ResultSet rs = pstm.executeQuery();

			while (rs.next()) {
				f.setId(rs.getInt("id"));
				f.setNome(rs.getString("nome"));
				f.setCpf(rs.getString("cpf"));
				f.setComissao(rs.getDouble("comissao"));
				f.setDataInclusao(rs.getDate("datainclusao"));
				f.setStatus(rs.getString("status"));
				f.setSetor(rs.getString("setor"));
			}

			pstm.close();
			conexao.close();
		} catch (Exception e) {
			System.out.println(e);
		}

		return f;

	}

	@Override
	public List<Funcionario> listar() {
		Connection conexao = null;
		List<Funcionario> funcionarios = new ArrayList<Funcionario>();
		Funcionario f = new Funcionario();

		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao
					.prepareStatement("SELECT id, nome, cpf, comissao, datainclusao, status, setor FROM tbfuncionario");

			ResultSet rs = pstm.executeQuery();

			while (rs.next()) {
				f.setId(rs.getInt("id"));
				f.setNome(rs.getString("nome"));
				f.setCpf(rs.getString("cpf"));
				f.setComissao(rs.getDouble("comissao"));
				f.setDataInclusao(rs.getDate("datainclusao"));
				f.setStatus(rs.getString("status"));
				f.setSetor(rs.getString("setor"));

				funcionarios.add(f);
			}

			pstm.close();
			conexao.close();
		} catch (Exception e) {
			System.out.println(e);
		}

		return funcionarios;
	}
}
